package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.BomInfoListAdapter;
import com.glory.brepda.adapter.GroupEqpListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.BomInfoEntity;
import com.glory.brepda.entity.EqpStatusEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class EquipmentStatusQueryActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.teEqpStatu)
    TextView teEqpStatu;
    @BindView(R.id.teEqpHoldStatu)
    TextView teEqpHoldStatu;
    @BindView(R.id.teEqpActionCode)
    TextView teEqpActionCode;
    @BindView(R.id.teEqpActionReason)
    TextView teEqpActionReason;
    @BindView(R.id.teisIdle)
    TextView teisIdle;
    @BindView(R.id.teEqpActionComment)
    TextView teEqpActionComment;
    @BindView(R.id.teWorkLots)
    TextView teWorkLots;
    @BindView(R.id.tepmsNames)
    TextView tepmsNames;
    @BindView(R.id.rvEqpGroupList)
    RecyclerView rvEqpGroupList;



    private GroupEqpListAdapter myAdapter;

    private List<EqpStatusEntity.ResultBean> groupEqpList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_status_query;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("查询");
        initRecyclerView(rvEqpGroupList,this);
        myAdapter = new GroupEqpListAdapter(null,false);
        myAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        myAdapter.isFirstOnly(true);
        rvEqpGroupList.setAdapter(myAdapter);
    }

    @Override
    protected void initData() {
        initEdit(etEqpId, this::getEqpStatus);
    }

    private void getEqpStatus(String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<EqpStatusEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EqpStatusEntity>>
                (new TypeToken<BaseResponseBean<EqpStatusEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpStatusEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EqpStatusEntity body = response.body().getResponse().getBody();
                    teisIdle.setText("Y".equals(body.getResult().getIsIdle())?"是":"否");
                    if(body.getResult().getSTATU().equals("IDLE")){
                        teEqpStatu.setText("待机");
                    }
                    if(body.getResult().getSTATU().equals("RUN") || (body.getResult().getSTATU().equals("RUN2"))){
                        findViewById(R.id.workLots_layout).setVisibility(View.VISIBLE);
                        teEqpStatu.setText("运行中");
                        teWorkLots.setText(body.getResult().getWORKLOTS());
                    }else{
                        findViewById(R.id.workLots_layout).setVisibility(View.GONE);
                    }
                    if (body.getResult().getSTATU().equals("PM")){
                        teEqpStatu.setText("保养中");
                        findViewById(R.id.pms_layout).setVisibility(View.VISIBLE);
                        tepmsNames.setText(body.getResult().getPmsSheetDesc());
                    }else{
                        findViewById(R.id.pms_layout).setVisibility(View.GONE);
                    }
                    if(body.getResult().getHOLDSTATU().equals("On")){
                        teEqpHoldStatu.setText("暂扣");
                        if(!body.getResult().getACTIONCODE().equals("无数据")){
                            findViewById(R.id.code_layout).setVisibility(View.VISIBLE);
                            teEqpActionCode.setText(body.getResult().getACTIONCODE());
                        }
                        if(!body.getResult().getACTIONREASON().equals("无数据")){
                            findViewById(R.id.reason_layout).setVisibility(View.VISIBLE);
                            teEqpActionReason.setText(body.getResult().getACTIONREASON());
                        }
                        if(!body.getResult().getACTIONCOMMENT().equals("无数据")){
                            findViewById(R.id.comment_layout).setVisibility(View.VISIBLE);
                            teEqpActionComment.setText(body.getResult().getACTIONCOMMENT());
                        }
                    }else {
                        teEqpHoldStatu.setText("未暂扣");
                        findViewById(R.id.code_layout).setVisibility(View.GONE);
                        findViewById(R.id.reason_layout).setVisibility(View.GONE);
                        findViewById(R.id.comment_layout).setVisibility(View.GONE);
                    }
                    groupEqpList.clear();
                    if (!body.getEQPGROUP().isEmpty()){
                        for (EqpStatusEntity.ResultBean entity : body.getEQPGROUP()){
                            if (!groupEqpList.contains(entity)){
                                groupEqpList.add(entity);
                            }
                        }
                    }


                    myAdapter.setNewData(groupEqpList);
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<EqpStatusEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEquipment");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID",eqpID);
        map.addProperty("ACTIONTYPE","ALL");
        NetUtils.requestPostNet("IF.BREEQPSTATUSQUERY","Common", EquipmentStatusQueryActivity.this,map,callBack);
    }
}
